/*
 * ContextExample.java
 *
 * Created on August 25, 2005, 2:44 PM
 * This class is contains common functionality for all examples
 * that require Metadata lookup. This is done to simplify the source code
 * of individual examples.
 */

package oracle.olapi.examples;
import java.util.Properties;

import oracle.express.olapi.data.full.ExpressDataProvider;
import oracle.olapi.data.source.FundamentalMetadataProvider;
import oracle.olapi.metadata.mdm.MdmMeasure;
import oracle.olapi.metadata.mdm.MdmMetadataProvider;
import oracle.olapi.metadata.mdm.MdmPrimaryDimension;
import oracle.olapi.transaction.NotCommittableException;
import oracle.olapi.transaction.NotPreparedException;
import oracle.olapi.transaction.Transaction;
import oracle.olapi.transaction.TransactionInactiveException;
import oracle.olapi.transaction.TransactionProvider;

/**
 * This class serves as the root of all example tests to make
 * routine operations simpler.
 * @author Oracle Corporation
 */
abstract public class ContextExample extends BaseExample {
    
    private Context10g context;
    
    protected Context10g getContext() {
        return context;
    }
    
    public ContextExample() {}
    
    protected void initialize(Properties properties)
    throws Exception {
        super.initialize(properties);
        createContext(properties);
    }
    
    protected void shutdown() {
        closeContext();
        super.shutdown();
    }
    
    protected void createContext(Properties properties) {
        context = new Context10g(properties, getLogger());
    }
    
    protected void closeContext() {
        try {
            context.close();
        } catch ( Exception e ) {} // ignore
        finally {
            context = null;
        }
    }
    
    protected MdmPrimaryDimension getMdmPrimaryDimension(String name) {
        return context.getMdmPrimaryDimensionByName(name);
    }
    
    protected MdmMeasure getMdmMeasure(String name) {
        return context.getMdmMeasureByName(name);
    }
    
    protected ExpressDataProvider getExpressDataProvider() {
        return context.getDataProvider();
    }
    
    protected MdmMetadataProvider getMdmMetadataProvider() {
        return context.getMetadataProvider();
    }
    
    protected FundamentalMetadataProvider getFundamentalMetadataProvider() {
        return context.getFundamentalMetadataProvider();
    }
    
    protected TransactionProvider getTransactionProvider() {
        return context.getTransactionProvider();
    }
    
    /**
     * This method performs a 'prepare and commit'
     */
    protected void prepareAndCommit()
    throws NotCommittableException, TransactionInactiveException {
        TransactionProvider tp = getTransactionProvider();
        tp.prepareCurrentTransaction();
        tp.commitCurrentTransaction();
    }
    
    protected void rollback() throws NotPreparedException {
        getTransactionProvider().rollbackCurrentTransaction();
    }
    
    protected void beginSubtransaction() throws TransactionInactiveException {
        getTransactionProvider().beginSubtransaction();
    }
    
    protected Transaction getCurrentTransaction() {
        return getTransactionProvider().getCurrentTransaction();
    }
}
